package com.wangyang.controller;

import com.wangyang.pojo.Category;
import com.wangyang.pojo.Result;
import com.wangyang.service.CategoryService;
import jakarta.validation.constraints.NotNull;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result addCategory(@RequestBody @Validated(Category.Add.class) Category category){
        return categoryService.addCategory(category);
    }

    @GetMapping
    public Result getAllCategory(){
        return categoryService.getAllCategory();
    }

    @GetMapping("/detail")
    public Result getCategoryDetail(@Param("id") @NotNull Integer id){
        return categoryService.getCategoryDetail(id);
    }

    @PutMapping
    public Result updateCategory(@RequestBody @Validated(Category.Update.class) Category category){
        return categoryService.updateCategory(category);
    }

    @DeleteMapping
    public Result deleteCategory(@Param("id") @NotNull Integer id){
        return categoryService.deleteCategory(id);
    }
}
